package cn.cms.exception;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 控制层异常公共处理类
 */
@Component
public class CustomHandlerExceptionResolver implements HandlerExceptionResolver {

    private static Logger logger = LoggerFactory.getLogger(CustomHandlerExceptionResolver.class);

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        logger.info("请求地址：");
        logger.info(httpServletRequest.getRequestURL().toString());
        logger.info("报错位置：");
        logger.info("Object:" + o);
        logger.info("报错信息：");
        logger.info("", e);
        // 系统错误
        httpServletResponse.setHeader("Error-Json", "{\"code\":\"12001\"}");
        return null;
    }
}
